package org.example.user.controller;

import com.mybatisflex.core.paginate.Page;
import jakarta.validation.Valid;
import org.example.common.config.Result;
import org.example.common.enums.LogAsync;
import org.example.user.dto.CreateAclDTO;
import org.example.user.dto.UpdateAclDTO;
import org.example.user.dto.AclQueryDTO;
import org.example.user.service.ISysAclService;
import org.example.user.vo.SysAclVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/acl")
public class SysAclController {

    @Autowired
    private ISysAclService sysAclService;

    @LogAsync("创建权限点")
    @PostMapping("/create")
    public Result create(@RequestBody @Valid CreateAclDTO createAclDTO) {
        int result = sysAclService.createAcl(createAclDTO);
        return Result.ok(result);
    }

    @LogAsync("修改权限点")
    @PutMapping("/update")
    public Result update(@RequestBody @Valid UpdateAclDTO updateAclDTO) {
        int result = sysAclService.updateAcl(updateAclDTO);
        return Result.ok(result);
    }

    @LogAsync("删除权限点")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long aclId) {
        int result = sysAclService.deleteAcl(aclId);
        return Result.ok(result);
    }

    @GetMapping("/list")
    public Result list(@ModelAttribute AclQueryDTO aclQueryDTO) {
        Page<SysAclVO> list = sysAclService.pageList(aclQueryDTO);
        return Result.ok(list);
    }

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable("id") Long aclId) {
        SysAclVO detail = sysAclService.getAclDetail(aclId);
        return Result.ok(detail);
    }

    @GetMapping("/tree")
    public Result tree() {
        List<SysAclVO> tree = sysAclService.getAclTree();
        return Result.ok(tree);
    }

    @PostMapping("/linkMenu")
    public Result linkMenu(@RequestParam Long aclId, @RequestParam Long menuId) {
        int result = sysAclService.linkMenu(aclId, menuId);
        return Result.ok(result);
    }
}
